<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let pay_type_map = {
      wechat: '微信支付',
      wechat_partner: '微信服务商',
    }
    let relation_type_map = {
      'SERVICE_PROVIDER': '服务商',
      'STORE': '门店',
      'STAFF': '员工',
      'STORE_OWNER': '店主',
      'PARTNER': '合作伙伴',
      'HEADQUARTER': '总部',
      'BRAND': '品牌方',
      'DISTRIBUTOR': '分销商',
      'USER': '用户',
      'SUPPLIER': '供应商',
    }
    let forms_config = {
      account: {label:'商户号',name:'account',type:'text_one',tips:'分账接收方的商户号'},
      name: {label:'商户名',name:'name',type:'text_one',tips:'分账接收方的商户名，需要 商户全称'},
      pay_type: {label:'支付类型',name:'pay_type',type:'select_one',config:pay_type_map,tips:'指定此分账在什么支付方式中生效',value:'wechat_partner'},
      relation_type: {label:'关系类型',name:'relation_type',type:'select_one',config:relation_type_map,tips:'分账接收方 是 分账出资方的 XXX(关系)'},
      tenant_id: {label:'租户',name:'tenant_id',type:'select_one',config:[],tips:'', remove:true}
    }
    if (id){
      s94Mjs.each(forms_config, function (row, k){
        if (k != 'sharing_ratio') row.is_info = true;
      })
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/sharing_receiver/edit?id=`+this.id : '/admin/sharing_receiver/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('sharing_receiver/index', '$refs.list_url.list');
          iframe.hide('sharing_receiver/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })

    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/sharing_receiver/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('sharing_receiver/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    if (!_config('this_tenant.id')){
      this.config.tenant_id.remove = false;
      ajax({url:'/admin/index/tenant_list', cache:true}, (res)=>{
        this.config.tenant_id.config = decode_config(res.data.list, {content_key:'title', value_key:'id'});
        return false;
      })
    }
  }
}
</script>
